package com.imdroid.logic;

import java.util.Collection;
import java.util.Date;

import net.orange.architecture.db.Condition;
import net.orange.architecture.db.Conditions;
import net.orange.architecture.db.Operator;
import net.orange.architecture.db.Relation;
import net.orange.architecture.process.BaseProcess;
import net.orange.architecture.process.ProcessFactory;

import com.imdroid.modal.Device;
import com.imdroid.modal.Location;

/**
 * The process interface of IMDroid devices
 * 
 */
public class DeviceProcess extends BaseProcess implements IDeviceProcess {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#getDeviceByUserId(int)
	 */
	public Device getDeviceByUserId(int user_id) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("user_id", user_id));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#getDeviceByIMEI(java.lang.String)
	 */
	public Device getDeviceByIMEI(String IMEI) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("imei_number", IMEI));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.logic.IDeviceProcess#getDeviceByUserName(java.lang.String)
	 */
	public Device getDeviceByUserName(String user_name) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("user_name", user_name));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.logic.IDeviceProcess#getDeviceByUserName(java.lang.String,
	 * int)
	 */
	public Device getDeviceByUserName(String user_name, int type)
			throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("user_name", user_name));
		cnds.add(new Condition("type", type));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#getDeviceByMobile(java.lang.String,
	 * int)
	 */
	public Device getDeviceByMobile(String mobile_phone, int type)
			throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("mobile_phone", mobile_phone));
		cnds.add(new Condition("type", type));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#getDeviceByParent(int)
	 */
	public Device getDeviceByParent(int parentId) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("parentId", parentId));
		cnds.add(new Condition("type", Device.TYPE_DEVICE_LITE));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.logic.IDeviceProcess#getDeviceByNickname(java.lang.String)
	 */
	public Device getDeviceByNickname(String nickname) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("nickname", nickname));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#searchUser(java.lang.String)
	 */
	public Collection searchUser(String keywork, int userId) throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("nickname", new Operator(Operator.LIKE), "%"
				+ keywork + "%"));
		cnds.add(new Condition("user_id", new Operator(Operator.NOT_EQUAL),
				userId));
		return dao.list(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#searchTeam(java.lang.String, int)
	 */
	public Collection searchTeam(String keywork, int userId) throws Exception {
		Conditions cnds = new Conditions();
		// cnds.add(new Condition("user_id", new Operator(Operator.NOT_EQUAL),
		// userId));
		cnds.add(new Condition("nickname", new Operator(Operator.LIKE), "%"
				+ keywork + "%"), new Relation(Relation.OR));
		cnds.add(new Condition("mobile_phone", new Operator(Operator.LIKE), "%"
				+ keywork + "%"), new Relation(Relation.OR));
		cnds.add(new Condition("board_plate", new Operator(Operator.LIKE), "%"
				+ keywork + "%"), new Relation(Relation.OR));

		return dao.list(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.logic.IDeviceProcess#getDeviceByNameAndPwd(java.lang.String,
	 * java.lang.String)
	 */
	public Device getUserByUserNameAndPwd(String userName, String password)
			throws Exception {
		Conditions cnds = new Conditions();
		cnds.add(new Condition("user_name", userName));
		cnds.add(new Condition("user_password", password));
		return (Device) dao.find(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imdroid.logic.IDeviceProcess#updateLocation(java.lang.String,
	 * float, float, long)
	 */
	public void updateLocation(String IMEI, float lat, float lon,
			long start_time) throws Exception {
		Device device = getDeviceByIMEI(IMEI);
		if (device != null) {
			// update the current location
			device.setCurrent_lat(lat);
			device.setCurrent_lon(lon);
			device.setCurrent_loc_time(new Date());
			update(device);
			// insert into the history table
			ILocationProcess locationProcess = (ILocationProcess) ProcessFactory
					.get(Location.class);
			Location location = new Location();
			location.setDevice(device);
			location.setPos_date(new Date());
			location.setPos_lat(lat);
			location.setPos_lon(lon);
			location.setStart_time(start_time);
			locationProcess.insert(location);
		}
	}

}
